﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Furniture_Rental_System
{
    public partial class MainForm : Form
    {

        String currentLoggedInEmployeeId;

        public MainForm()
        {
            InitializeComponent();
        }

        #region EVENT LISTENERS

        /////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////// EVENT LISTENERS //////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////
      
        private void buttonLogIn_Click(object sender, EventArgs e)
        {
            string logInUserName = this.textBoxUserName.Text;
            string logInPassword = this.textBoxPassword.Text;
            
            //Creates new DataBaseAccessor
            DataBaseAccessor accessorForLogin = new DataBaseAccessor();

            //Calls checkDataBaseForEmployeeIDAndPassword() to validate employee login.
            if (accessorForLogin.checkDataBaseForAdminIDAndPassword(logInUserName, logInPassword) &&
                this.buttonLogIn.Text == "Log In")
            {
                setComponentsForLogIntoTheSystem();
                setComponentsForLogIntoTheSystemAdmin();
                this.currentLoggedInEmployeeId = logInUserName;
            }
            else if (accessorForLogin.checkDataBaseForEmployeeIDAndPassword(logInUserName, logInPassword) &&
                this.buttonLogIn.Text == "Log In")
            {
                setComponentsForLogIntoTheSystem();
                this.currentLoggedInEmployeeId = logInUserName;
            }
            else
            {
                setComponentsForLogOutOfTheSystem();
                this.currentLoggedInEmployeeId = "";
            }

        }

        private void buttonMemberLookup_Click(object sender, EventArgs e)
        {
            setComponentsForMemberLookup();
        }

        private void buttonSearchForMember_Click(object sender, EventArgs e)
        {
            if (this.textBoxAreaCode.Text + this.textBoxPhoneFirstThree.Text + this.textBoxPhoneLastFour.Text == "")
            {
                MessageBox.Show("Please enter phone Number");
            }
            else
            {
                DataBaseAccessor dataBase = new DataBaseAccessor();
                List<string> returnedMember = new List<string>();
                returnedMember = dataBase.memberLookUpByPhoneNumber(this.textBoxAreaCode.Text + this.textBoxPhoneFirstThree.Text + this.textBoxPhoneLastFour.Text);
                if (returnedMember.Count!=0)
                {
                    returnedMember = dataBase.memberLookUpByPhoneNumber(this.textBoxAreaCode.Text + this.textBoxPhoneFirstThree.Text + this.textBoxPhoneLastFour.Text);
                    this.textBoxMemberID.Text = returnedMember[0];
                    this.textBoxFirstName.Text = returnedMember[1];
                    this.textBoxLastName.Text = returnedMember[2];
                    this.textBoxSSN.Text = returnedMember[3];
                    this.textBoxAreaCode.Text = returnedMember[4].Substring(0, 3);
                    this.textBoxPhoneFirstThree.Text = returnedMember[4].Substring(3, 3);
                    this.textBoxPhoneLastFour.Text = returnedMember[4].Substring(6, 4);
                    this.textBoxHouseNumber.Text = returnedMember[5];
                    this.textBoxStreet.Text = returnedMember[6];
                    this.textBoxCity.Text = returnedMember[7];
                    this.comboBoxState.Text = returnedMember[8];
                    this.textBoxZipCode.Text = returnedMember[9];
                    this.groupBoxMemberDisplay.Visible = true;
                }
                else
                    MessageBox.Show("No members found with that Number");
            }     
        }
        private void buttonAddMember_Click(object sender, EventArgs e)
        {
            setComponentsForCreateNewMember();
        }

        private void buttonCreateMembership_Click(object sender, EventArgs e)
        {
            String firstName = this.textBoxFirstName.Text;
            String lastName = this.textBoxLastName.Text;
            String ssn = this.textBoxSSN.Text;
            String house = this.textBoxHouseNumber.Text;
            String street = this.textBoxStreet.Text;
            String city = this.textBoxCity.Text;
            String state = this.comboBoxState.Text;
            String zip = this.textBoxZipCode.Text;
            String phone = this.textBoxAreaCode.Text + this.textBoxPhoneFirstThree.Text + this.textBoxPhoneLastFour.Text;

            // Calls validateNewMemberInfo to check for empty strings
            if (validateNewMemberInfo(firstName, lastName, ssn, phone, house, street, city, state, zip))
            {
                // Creates new DataBaseAccessor
                DataBaseAccessor accessorForNewMember = new DataBaseAccessor();
                
                // Calls addNewMember() method to add member to the database.
                if (accessorForNewMember.addNewMember(firstName, lastName, ssn, phone, house, street, city, state, zip))
                {
                    setComponentsForCreateNewMember();
                    MessageBox.Show("Member has been added.");
                    return;
                }
                else
                {
                    MessageBox.Show("Could not add the member.");
                }
 
            }
            
        }

        private void buttonFurnitureLookup_Click(object sender, EventArgs e)
        {
            setComponentsForFurnitureLookup();
        }

        private void buttonSearchFurniture_Click(object sender, EventArgs e)
        {
            //user is adding furniture item
            if (this.buttonSearchFurniture.Text == "Add item")
            {
                if (addFurnitureItem())
                {
                    MessageBox.Show("Item has been added.");
                    this.textBoxManufacturer.Text = "";
                    this.textBoxStyle.Text = "";
                    this.textBoxCategory.Text = "";
                    this.textBoxFineRate.Text = "";
                    this.textBoxColor.Text = "";
                }
            }
            //user is seaching for furniture
            else 
            {
                listViewFurniture.Items.Clear();
                searchForTheFurniture();
            }
            
        }

        private void buttonRent_Click(object sender, EventArgs e)
        {
            RentItemForm rent = new RentItemForm(this.currentLoggedInEmployeeId);
            rent.Show();
        }

        private void buttonSearchMemHistory_Click(object sender, EventArgs e)
        {

            try
            {
                String theMemId = this.textBoxMemId.Text;

                if (theMemId == "")
                {
                    MessageBox.Show("Must enter the Member Id.");
                    return;
                }
                else
                {
                    DataBaseAccessor searchMemberHistory = new DataBaseAccessor();

                    List<ListViewItem> returnedList = new List<ListViewItem>();

                    returnedList = searchMemberHistory.memberHistory(theMemId);

                    if (returnedList.Count != 0)
                    {
                        //Runs through the returned list of ListViewItems and adds them to the ListView
                        for (int i = 0; i < returnedList.Count; i++)
                        {
                            this.listViewMemberHistory.Items.Add(returnedList[i]);
                        }
                    }
                    else
                    {
                        MessageBox.Show("No results found for given criteria.");
                        this.textBoxMemId.Text = "";
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("No results found for given criteria.");
                this.textBoxMemId.Text = "";
            }

        }

        private void buttonSysAdminSQL_Click(object sender, EventArgs e)
        {
            SysAdminForm theSysAdminForm = new SysAdminForm();
            theSysAdminForm.Show();
        }

        private void buttonAddFurnitureItem_Click(object sender, EventArgs e)
        {
            this.groupBoxFurnitureInfo.Visible = true;
            this.labelItemNumber.Visible = false;
            this.textBoxItemNumber.Visible = false;
            this.buttonSearchFurniture.Text = "Add item";
            this.buttonSearchFurniture.Visible = true;
        }

        private void buttonReturnFurniture_Click(object sender, EventArgs e)
        {
            ReturnItemForm theReturnItemForm = new ReturnItemForm(this.currentLoggedInEmployeeId);
            theReturnItemForm.Show();
        }

        private void textBoxMemId_TextChanged(object sender, EventArgs e)
        {
            this.listViewMemberHistory.Items.Clear();
        }

        #endregion

        #region PRIVATE HELPERS

        /////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////// PRIVATE HELPERS //////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Checks if a value has been entered for all of the member information fields.
        /// </summary>
        /// Member Information: <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="ssn"></param>
        /// <param name="phone"></param>
        /// <param name="house"></param>
        /// <param name="street"></param>
        /// <param name="city"></param>
        /// <param name="state"></param>
        /// <param name="zip"></param>
        /// <returns></returns>
        private static bool validateNewMemberInfo(String firstName, String lastName, String ssn, String phone, String house, String street, String city, String state, String zip)
        {
            if (firstName == "")
            {
                MessageBox.Show("Must Enter First Name.");
                return false;
            }
            if (lastName == "")
            {
                MessageBox.Show("Must Enter Last Name.");
                return false;
            }
            if (ssn == "")
            {
                MessageBox.Show("Must Enter SSN.");
                return false;
            }
            if (ssn.Length != 9)
            {
                MessageBox.Show("SSN must be 9 characters.");
                return false;
            }
            if (phone.Length != 10)
            {
                MessageBox.Show("Phone Number is Incorrect.");
                return false;
            }
            if (house == "")
            {
                MessageBox.Show("Must Enter House Number.");
                return false;
            }
            if (street == "")
            {
                MessageBox.Show("Must Enter Street.");
                return false;
            }
            if (city == "")
            {
                MessageBox.Show("Must Enter City.");
                return false;
            }
            if (state == "")
            {
                MessageBox.Show("Must Enter State.");
                return false;
            }
            if (state.Length != 2)
            {
                MessageBox.Show("State is Incorrect.");
                return false;
            }
            if (zip == "")
            {
                MessageBox.Show("Must Enter Zip Code.");
                return false;
            }
            if (zip.Length != 5)
            {
                MessageBox.Show("Zip Code is Incorrect.");
                return false;
            }

            return true;
        }

        /// <summary>
        /// Prepares the two string Arrays that DataBaseAccessor uses to query furniture look up.
        /// </summary>

        /// <summary>
        /// Prepares the two string Arrays that DataBaseAccessor uses to query furniture look up.
        /// </summary>
        private void searchForTheFurniture()
        {
            String itemNumber = this.textBoxItemNumber.Text;
            String fineRate = this.textBoxFineRate.Text;
            String manufacturer = this.textBoxManufacturer.Text;
            String color = this.textBoxColor.Text;
            String style = this.textBoxStyle.Text;
            String category = this.textBoxCategory.Text;
            List<ListViewItem> returnedList = new List<ListViewItem>();
            if ((itemNumber == "") && (fineRate == "") && (manufacturer == "") &&
                (color == "") && (style == "") && (category == ""))
            {
                MessageBox.Show("Must enter at least one search field.");
                return;
            }
            else
            {
                String[] types = new String[6];
                String[] values = new String[6];

                if (itemNumber != "")
                {
                    types[0] = "itemNumber";
                    values[0] = itemNumber;
                }

                if (fineRate != "")
                {
                    types[1] = "fineRate";
                    values[1] = fineRate;
                }

                if (manufacturer != "")
                {
                    types[2] = "Manufacturer";
                    values[2] = manufacturer;
                }

                if (color != "")
                {
                    types[3] = "Color";
                    values[3] = color;
                }

                if (style != "")
                {
                    types[4] = "Style";
                    values[4] = style;
                }

                if (category != "")
                {
                    types[5] = "Category";
                    values[5] = category;
                }

                // Creates new DataBaseAccessor and passes in the string arrays.
                DataBaseAccessor dataBaseAccessorForFurnitureQuery = new DataBaseAccessor();
                returnedList = dataBaseAccessorForFurnitureQuery.furnitureLookUp(types, values);

                if (returnedList.Count != 0)
                {
                    //Runs through the returned list of ListViewItems and adds them to the ListView
                    listViewFurniture.Visible = true;
                    for (int i = 0; i < returnedList.Count; i++)
                    {
                        this.listViewFurniture.Items.Add(returnedList[i]);
                    }
                }
                else
                    MessageBox.Show("No results found for given criteria.");
            }
        }

        private bool addFurnitureItem()
        {

            String fineRate = this.textBoxFineRate.Text;
            String manufacturer = this.textBoxManufacturer.Text;
            String color = this.textBoxColor.Text;
            String style = this.textBoxStyle.Text;
            String category = this.textBoxCategory.Text;

            if ((fineRate == "") || (manufacturer == "") ||
                (color == "") || (style == "") || (category == ""))
            {
                MessageBox.Show("Must enter all of the information to add the new item.");
                return false;
            }
            else
            { 
                DataBaseAccessor addFurnitureItem = new DataBaseAccessor();
                addFurnitureItem.addFurnitureItem(fineRate, manufacturer, color, style, category);
                return true;
            }
 
            
        }

        #endregion

        #region Component Setters

        /////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////// COMPONENT SETTERS ////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////

        private void setComponentsForLogIntoTheSystem()
        {
            this.labelUserName.Visible = false;
            this.textBoxUserName.Visible = false;

            this.labelPassword.Visible = false;
            this.textBoxPassword.Visible = false;

            this.buttonLogIn.Text = "Log out";

            this.tabControlMain.Visible = true;

            //this.comboBoxState.

        }

        private void setComponentsForLogIntoTheSystemAdmin()
        {
            //this.labelUserName.Visible = false;
            //this.textBoxUserName.Visible = false;

            //this.labelPassword.Visible = false;
            //this.textBoxPassword.Visible = false;

            //this.buttonLogIn.Text = "Log out";

            //this.tabControlMain.Visible = true;

            this.buttonSysAdminSQL.Visible = true;
            this.labelSystemAdmin.Visible = true;
            
        }

        private void setComponentsForLogOutOfTheSystem()
        {
            this.labelUserName.Visible = true;
            this.textBoxUserName.Text = "";
            this.textBoxUserName.Visible = true;

            this.labelPassword.Visible = true;
            this.textBoxPassword.Text = "";
            this.textBoxPassword.Visible = true;

            this.buttonLogIn.Text = "Log In";

            this.tabControlMain.Visible = false;
            this.buttonSysAdminSQL.Visible = false;
            this.labelSystemAdmin.Visible = false;

        }

        private void setComponentsForMemberLookup()
        {
            this.groupBoxMemberInfo.Visible = true;
            this.groupBoxPhoneNumber.Visible = true;
            this.buttonSearchForMember.Visible = true;
            this.groupBoxMemberDisplay.Visible = false;
            this.buttonCreateMembership.Visible = false;
            this.textBoxAreaCode.Text = "";
            this.textBoxPhoneFirstThree.Text = "";
            this.textBoxPhoneLastFour.Text = "";
            this.textBoxMemberID.Text = "";
            this.textBoxFirstName.Text = "";
            this.textBoxLastName.Text = "";
            this.textBoxHouseNumber.Text = "";
            this.textBoxStreet.Text = "";
            this.textBoxCity.Text = "";
            this.comboBoxState.Text = "";
            this.textBoxZipCode.Text = "";
            this.textBoxSSN.Text = "";
            this.textBoxAreaCode.Focus();
        }

        private void setComponentsToDisplayTheMember(string[] theMember)
        {
            this.buttonSearchForMember.Visible = false;
            this.groupBoxMemberDisplay.Visible = true;

            this.textBoxMemberID.Text = theMember[0];
            this.textBoxFirstName.Text = theMember[1];
            this.textBoxLastName.Text = theMember[2];
            this.textBoxHouseNumber.Text = theMember[3];
            this.textBoxStreet.Text = theMember[4];
            this.textBoxCity.Text = theMember[5];
            this.comboBoxState.Text = theMember[6];
            this.textBoxZipCode.Text = theMember[7];
            this.textBoxSSN.Text = theMember[8];
        }

        private void setComponentsForMemberDoesNotExist()
        {
            MessageBox.Show("Member Phone Number Does Not Exist");
            this.textBoxAreaCode.Text = "";
            this.textBoxPhoneFirstThree.Text = "";
            this.textBoxPhoneLastFour.Text = "";
        }

        private void setComponentsForCreateNewMember()
        {
            this.groupBoxMemberInfo.Visible = true;
            this.buttonSearchForMember.Visible = false;
            this.textBoxMemberID.Visible = false;
            this.labelMemberID.Visible = false;
            this.groupBoxMemberDisplay.Visible = true;
            this.textBoxAreaCode.Text = "";
            this.textBoxPhoneFirstThree.Text = "";
            this.textBoxPhoneLastFour.Text = "";
            this.textBoxMemberID.Text = "";
            this.textBoxFirstName.Text = "";
            this.textBoxLastName.Text = "";
            this.textBoxHouseNumber.Text = "";
            this.textBoxStreet.Text = "";
            this.textBoxCity.Text = "";
            this.comboBoxState.Text = "";
            this.textBoxZipCode.Text = "";
            this.textBoxSSN.Text = "";
            this.buttonCreateMembership.Visible = true;
        }

        private void setComponentsForFurnitureLookup()
        {
            this.groupBoxFurnitureInfo.Visible = true;
            this.labelItemNumber.Visible = true;
            this.textBoxItemNumber.Visible = true;
            listViewFurniture.Visible = false;
        }

        #endregion

        
    //end of form 1 class
    }


//end of Furniture_Rental_System namespace
}
